#include "arcwidget.h"
#include "ui_arcwidget.h"
#include "mainwindow.h"
#include <QPainterPath>
#include <QString>
#include <QPainter>

ArcWidget::ArcWidget(QWidget *parent)
    : QWidget(parent),
    m_nPeopleCount(0),
    m_nCount(0)
{
    this->resize(600, 600);
}

ArcWidget::~ArcWidget()
{
}

void ArcWidget::paintEvent(QPaintEvent *e)
{
    type_name_StrList =type_name_Map.values();
    duration_List = duration_Map.values();
    m_nPeopleCount=0;
    m_nCount=0;
    foreach (int num, duration_List)
    {
        m_nPeopleCount += num;
        m_nCount++;
    }

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::NoBrush);
    //    painter.drawImage(this->rect(),m_bg);

    QRectF rect(-120, -120,
                (120 << 1), (120 << 1));
    QList<QColor> colorList;

    painter.translate(220, 255);
    qreal startAngle = 0;
    foreach (int num, duration_List)
    {
        //生成随机颜色并过滤掉白色
        int colorR, colorG, colorB;
        while(1)
        {
            colorR = rand()%256;
            colorG = rand()%256;
            colorB = rand()%256;
            if(colorR == 255 && colorG == 255 && colorB == 255){
                continue;
            }
            else{
                break;
            }
        }
        QColor color(colorR, colorG, colorB);
        colorList.append(color);

        painter.setPen(Qt::NoPen);
        painter.setBrush(QBrush(color));

        //该答案选项所占比例
        qreal percentNum = 0;percentNum=(qreal)num/(qreal)m_nPeopleCount;
        //所占比例对应的角度
        qreal arcLength = 0;arcLength=360*percentNum;

        //画扇形
        QPainterPath path;
        path.arcTo(rect, startAngle, arcLength);

        //扇形二等分线的角度
        qreal middleAngle =0;middleAngle= startAngle + arcLength/2;
        qreal radianNum = 0;

        if(middleAngle >= 0 && middleAngle < 90)
        {
            radianNum = middleAngle*m_pi/180;
        }
        else if(middleAngle >= 90 && middleAngle < 180)
        {
            radianNum = (180 - middleAngle)*m_pi/180;
        }
        else if(middleAngle >= 180 && middleAngle < 270)
        {
            radianNum = (middleAngle - 180)*m_pi/180;
        }
        else if(middleAngle >= 270 && middleAngle < 360)
        {
            radianNum = (360 - middleAngle)*m_pi/180;
        }

        //二等分线的中点距原点的距离
        qreal textPosY =0;textPosY= 60*sin(radianNum);
        qreal textPosX =0;textPosX= 60*cos(radianNum);

        //根据二等分线所在的象限确定二等分线的中点坐标
        if(middleAngle >= 0 && middleAngle < 90)
        {
            textPosY = -textPosY;
        }
        else if(middleAngle >= 90 && middleAngle < 180)
        {
            textPosX = -textPosX;
            textPosY = -textPosY;
        }
        else if(middleAngle >= 180 && middleAngle < 270)
        {
            textPosX = -textPosX;
        }

        startAngle += arcLength;
        QString percentStr = QString::number(percentNum*100 + 0.5);
        QString numStr = QString::number(num) +"Min";
        percentStr ="("+getPercentInt(percentStr)+ "%"+")" ;
        painter.drawPath(path);
        painter.setPen(QColor(Qt::white));
        painter.drawText(textPosX - 25, textPosY -30,
                         50, 20, Qt::AlignCenter, numStr);
        painter.drawText(textPosX - 25, textPosY -10,
                         50, 20, Qt::AlignCenter, percentStr);

    }
    painter.setBrush(Qt::NoBrush);
    painter.setPen(QColor(0, 0, 0));
    painter.translate(-220, -255);

    //绘制右侧的标注栏

    QFont ft;
    ft.setPixelSize(15);
    painter.setFont(ft);
    //这里创建了一个字体对象 ft，并设置其像素大小为 15 像素。
    //然后将这个字体应用到绘图对象 painter 上，以确保后续绘制的文本使用了指定的字体大小。

    int textStart = 160;
    foreach (QString answerStr, type_name_StrList)
    {
        painter.drawText(350, textStart,
                         200, 20,
                         Qt::AlignRight, answerStr);
        textStart += 30;
    }

    //颜色块
    textStart = 163;
    painter.setPen(Qt::NoPen);
    foreach (QColor color, colorList)
    {
        painter.setBrush(QBrush(color));
        painter.drawRect(552, textStart, 15, 15);
        textStart += 30;
    }

    //标题
    int textPosX = width() / 2;
    int textPosY = 35;
    QFont font;
    font.setPointSize(16);
    painter.setFont(font);
    painter.setPen(Qt::black);
    painter.drawText(textPosX - 100, textPosY, 200, 30, Qt::AlignCenter, "专注时长统计");

    //累计时长
    QString focusDurationText = QString("您已经使用番茄钟专注%1次，").arg(m_nCount)+QString("累计专注时长%2分钟").arg(m_nPeopleCount);
    int focusTextPosY = height() - 45;
    QFont focusFont;
    focusFont.setPointSize(16);
    painter.setFont(focusFont);
    painter.setPen(Qt::black);
    int focusTextPosX = (width() - painter.fontMetrics().horizontalAdvance(focusDurationText)) / 2;
    painter.drawText(focusTextPosX, focusTextPosY, focusDurationText);

    QWidget::paintEvent(e);

}

QString ArcWidget::getPercentInt(const QString &percentStr)
{
    QString percentIntStr;
    for(int i = 0;i < percentStr.size();++i)
    {
        if(percentStr.at(i) == '.')
        {
            break;
        }
        percentIntStr += percentStr.at(i);
    }
    return percentIntStr;
}
